<template>
	<view class="ab">
		<view :class="(cf? 'cf ' : '')+[styleType === 'text'?'segmented-control--text' : 'segmented-control--button' ]" :style="{ borderColor: styleType === 'text' ? '' : activeColor }" class="segmented-control">
			<view v-for="(item, index) in values" :class="[ styleType === 'text'?'segmented-control__item--text': 'segmented-control__item--button' , index === currentIndex&&styleType === 'button'?'segmented-control__item--button--active': '' , index === 0&&styleType === 'button'?'segmented-control__item--button--first': '',index === values.length - 1&&styleType === 'button'?'segmented-control__item--button--last': '' ]" :key="index" 
			:style="{
		    backgroundColor: index === currentIndex && styleType === 'button' ? activeColor : '',borderColor: index === currentIndex&&styleType === 'text'||styleType === 'button'?activeColor:activeColor
		  }" class="segmented-control__item " @click="_onClick(index)">
			
				<text :style="{color:
		      index === currentIndex
		        ? styleType === 'text'
		          ? activeColor
		          : '#fff'
		        : styleType === 'text'
		          ? '#000'
		          : activeColor}" :class="'segmented-control__text '+(index === currentIndex?'activetext':'')">{{ item.name }}
				  
					<text v-if="index !== currentIndex" class="en">{{ item.en }}</text>
				  </text>
			</view>
		</view>
	</view>
	
</template>

<script>
	/**
	 * SegmentedControl 分段器
	 * @description 用作不同视图的显示
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=54
	 * @property {Number} current 当前选中的tab索引值，从0计数
	 * @property {String} styleType = [button|text] 分段器样式类型
	 * 	@value button 按钮类型
	 * 	@value text 文字类型
	 * @property {String} activeColor 选中的标签背景色与边框颜色
	 * @property {Array} values 选项数组
	 * @event {Function} clickItem 组件触发点击事件时触发，e={currentIndex}
	 */

	export default {
		name: 'UniSegmentedControll',
		props: {
			current: {
				type: Number,
				default: 0
			},
			values: {
				type: Array,
				default () {
					return []
				}
			},
			activeColor: {
				type: String,
				default: '#007aff'
			},
			styleType: {
				type: String,
				default: 'button'
			}
		},
		data() {
			return {
				currentIndex: 0,
				cf:false
			}
		},
		watch: {
			current(val) {
				if (val !== this.currentIndex) {
					this.currentIndex = val
				}
			}
		},
		created() {
			this.currentIndex = this.current
		},
		
		methods: {
			//标题置顶
			_PageScroll(e){
				if (e.scrollTop >= 324){
					this.cf = true
				}
				else
					this.cf = false
			},
			_onClick(index) {
				if (this.currentIndex !== index) {
					this.currentIndex = index
					this.$emit('clickItem', {
						currentIndex: index
					})
				}
			},
			
		}
	}
</script>

<style scoped>
	.ab{
		position: absolute;
		top: 0;
		width: calc(100% - 40rpx);
		padding: 0 20rpx;
	}
	.tongue{
		position: absolute;
		left: 45%;
		bottom: -15px;
		line-height: 0;
		border-bottom: 20px solid #f3f3f3;
		
	}
	.segmented-control {
		/* #ifndef APP-NVUE */
		display: flex;
		box-sizing: border-box;
		/* #endif */
		flex-direction: row;
		height: 120rpx;
		overflow: hidden;
	}

	.segmented-control__item {
		/* #ifndef APP-NVUE */
		display: inline-flex;
		box-sizing: border-box;
		/* #endif */
		position: relative;
		flex: 1;
		justify-content: center;
		align-items: center;
	}

	.segmented-control__item--button {
		border-style: solid;
		border-top-width: 1px;
		border-bottom-width: 1px;
		border-right-width: 1px;
		border-left-width: 0;
	}

	.segmented-control__item--button--first {
		border-left-width: 1px;
		border-top-left-radius: 5px;
		border-bottom-left-radius: 5px;
	}

	.segmented-control__item--button--last {
		border-top-right-radius: 5px;
		border-bottom-right-radius: 5px;
	}

	.segmented-control__item--text {
		border-bottom-style: solid;
		border-bottom-width: 3px;
	}

	.segmented-control__text {
		font-size: 16px;
		line-height: 20px;
		text-align: center;
		font-weight: bold;
	}
	.segmented-control__text text{
		display: block;
	}
	.en{
		font-size: 22rpx;
		margin-top: 10rpx;
		color: #363636;
	}
	.activetext{
		font-size: 20px;
		position: relative;
	}
	.activetext::after{
		content: '';
		width: 15rpx;
		height: 15rpx;
		display: block;
		border: 3px solid #363636;
		background: #ffff;
		position: absolute;
		transform: rotate(45deg);
		bottom: -60rpx;
		left: 20rpx;
	}
	.cf{
		position: fixed;
		top: 57px;
		width: calc(100% - 40rpx);
		z-index: 999;
		background: #f3f3f3;
	}
	
</style>